package pl.soa.wawek.rest;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

import pl.soa.wawek.androidandrest.LoginPanel;

import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.ResultReceiver;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;

public class PostRest extends IntentService {

	private HttpClient httpClient;
	private HttpPost httpPost;
	private HttpResponse response;
	private HttpEntity entity;
	private String URL = "http://89.66.134.91:8080/RESTGateway/json/";
	private StringEntity s;
	private Handler h;
	private String result; // odpowiedz z gatewaya
	private JSONObject jsonobject;
	private model.ConnectionResponse connresp;

	public PostRest() {
		super("PostRestService");
	}

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		h = new Handler();
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		ResultReceiver rec = intent.getParcelableExtra("receiver");
		Bundle b = new Bundle();
		
		Bundle extras = intent.getExtras();
		String jsonstring = extras.getString("jsonobj");
		String urlclass = extras.getString("class");
		try {
			s = new StringEntity(jsonstring);
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}

		httpClient = new DefaultHttpClient();
		httpPost = new HttpPost(URL + urlclass + "/post");
		s.setContentEncoding("UTF-8");
		s.setContentType("application/json");
		httpPost.setEntity(s);
		httpPost.addHeader("accept", "application/json");

		try {
			response = httpClient.execute(httpPost);
			if (response.getStatusLine().getStatusCode() >= 300) {
				h.post(new Runnable() {
					@Override
					public void run() {
						Toast.makeText(getBaseContext(),
								"Error at server side", Toast.LENGTH_LONG)
								.show();
					}
				});
			} else {
				entity = response.getEntity();
				InputStream instream = null;
				instream = entity.getContent();
				result = GetRest.convertStreamToString(instream);
				jsonobject = new JSONObject(result);
				instream.close();
				connresp = new model.ConnectionResponse();
				connresp.setCode(jsonobject.getInt("code"));
				connresp.setMessage(jsonobject.getString("message"));
				b.putInt("code", connresp.getCode());
				rec.send(0, b); //wyslanie kodu na zewnatrz
				h.post(new Runnable() {
					@Override
					public void run() {
						Toast.makeText(getBaseContext(), connresp.toString(),
								Toast.LENGTH_LONG).show();
					}
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
